package com.klikmee.sms.chat.web.usecases;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import com.dotexe.webframework.core.ClientApplication;
import com.dotexe.webframework.core.clientactions.MessageBox;
import com.dotexe.webframework.core.datasources.CollectionDataSource;
import com.dotexe.webframework.core.triggers.Trigger;
import com.klikmee.sms.chat.web.dao.AgentDAO;
import com.klikmee.sms.chat.web.dao.AgentGroupDAO;
import com.klikmee.sms.chat.web.dao.GroupDAO;
import com.klikmee.sms.chat.web.util.SpringBeansLookup;
import com.klikmee.sms.services.chat.model.Agent;
import com.klikmee.sms.services.chat.model.AgentGroup;
import com.klikmee.sms.services.chat.model.Group;

public class GroupListForm extends ClientApplication {

	private CollectionDataSource dsGroups;
	private CollectionDataSource dsAgentsGroups;
	private ArrayList<String> bcGroups;
	private ArrayList<String> bcAgentsGroups;
	private GroupDAO groupDAO;
	private AgentDAO agentGroupDAO;
	private AgentGroupDAO agentsGroupDAO;
	private List<Group> groups;
	private List<Agent> groupAgents;
	private List<AgentGroup> groupAgent;
	private GroupDetailsForm details;
	private EmployeeListForm agentList;
	private AgentGroup ag;
	private boolean _selectionMode;
	private Agent selectedAgent;

	// override
	@Override
	public void load() throws Exception {
		super.load();

		this.dsGroups = new CollectionDataSource("dsGroups");
		this.getDataSources().add(this.dsGroups);
		this.bcGroups = new ArrayList<String>();
		this.bcGroups.add("GroupName");
		this.bcGroups.add("GroupDescription");
		this.bcGroups.add("Active");

		this.dsAgentsGroups = new CollectionDataSource("dsAgentsGroups");
		this.getDataSources().add(this.dsAgentsGroups);
		this.bcAgentsGroups = new ArrayList<String>();
		this.bcAgentsGroups.add("AgentFirstName");
		this.bcAgentsGroups.add("AgentLastName");

		this.triggers.add(new Trigger(this, "btnEditGroupClick"));
		this.triggers.add(new Trigger(this, "btnAddGroupClick"));
		this.triggers.add(new Trigger(this, "btnDeleteGroupClick"));
		this.triggers.add(new Trigger(this, "gvGroupsCellClick"));
		this.triggers.add(new Trigger(this, "gvGroupsCellDoubleClick"));

		this.triggers.add(new Trigger(this, "btnAddIntoGroupClick"));
		this.triggers.add(new Trigger(this, "btnDeleteFromGroupClick"));
		this.triggers.add(new Trigger(this, "gvAgentsGroupsCellClick"));
		this.triggers.add(new Trigger(this, "gvAgentsGroupsCellDoubleClick"));
	}

	@Override
	public void start() throws Exception {
		super.start();
		
		ChangeControlCollapsible("AppGroupListForm_borderLayout1_borderLayout1PanelRight", true);
		
		bindGroupsList();
		bindAgentsForGroup();
	}

	public void btnEditGroupClick() throws InstantiationException, IllegalAccessException, Exception {
		if (this.dsGroups.getSelectedItem() == null) {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati grupu!");
			return;
		} else {
			details = createChildApplication(GroupDetailsForm.class);
			details.setGroup((Group) this.dsGroups.getSelectedItem());
			details.triggers.get("btnSaveClick").addOnFire(this, "afterGroupSave");
			details.setEditGroup(true);
			this.showChildApp(details);
			details.start();
		}
	}

	public void btnAddGroupClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		details = createChildApplication(GroupDetailsForm.class);
		details.setGroup(new Group());
		details.triggers.get("btnSaveClick").addOnFire(this, "afterGroupSave");
		details.setEditGroup(false);
		this.showChildApp(details);
		details.start();
	}

	public void btnDeleteGroupClick() throws JSONException, SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
		if (this.dsGroups.getSelectedItem() == null) {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati grupu!");
			return;
		} else {
			Group group = (Group) this.dsGroups.getSelectedItem();
			group.setActive(false);
			groupDAO.edit(group);
			bindGroupsList();
		}
	}

	public void gvGroupsCellClick() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		bindAgentsForGroup();
		ChangeControlCollapsible("AppGroupListForm_borderLayout1_borderLayout1PanelRight", false);
	}

	public void gvGroupsCellDoubleClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		if (get_selectionMode()) {
			// parent will handle this!
		} else {
			details = createChildApplication(GroupDetailsForm.class);
			details.setGroup((Group) this.dsGroups.getSelectedItem());
			details.triggers.get("btnSaveClick").addOnFire(this, "afterGroupSave");
			details.setEditGroup(true);
			this.showChildApp(details);
			details.start();
		}
	}

	public void btnAddIntoGroupClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		if (this.dsGroups.getSelectedItem() == null) {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati grupu!");
			return;
		} else {
			agentList = createChildApplication(EmployeeListForm.class);
			agentList.triggers.get("gvAgentsCellDoubleClick").addOnFire(this, "afterAgentSelected");
			agentList.set_selectionMode(true);
			this.showChildApp(agentList);
			agentList.start();
		}
	}

	public void btnDeleteFromGroupClick() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		if(!groupAgent.isEmpty())
		{
			agentsGroupDAO.delete(groupAgent.get(0));
			bindAgentsForGroup();
		}
		else{
			MessageBox.Show(this, "Obavestenje", "Morate selektovati agenta!");
			return;
		}
	}

	public void gvAgentsGroupsCellClick() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		
		agentsGroupDAO = SpringBeansLookup.getBean(AgentGroupDAO.class);
		Group selectedGroup = (Group) this.dsGroups.getSelectedItem();
		Agent selectedAgent = (Agent) this.dsAgentsGroups.getSelectedItem();
		AgentGroup check = agentsGroupDAO.findByAgentAndGroup(selectedAgent, selectedGroup);
		
		groupAgent.add(check);
	}

	public void gvAgentsGroupsCellDoubleClick() {
	}
	
	public void afterGroupSave() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		bindGroupsList();
	}

	public void afterAgentSelected() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		agentsGroupDAO = SpringBeansLookup.getBean(AgentGroupDAO.class);
		Agent selectedAgent = (Agent) ((CollectionDataSource) (agentList.getDataSources().get("dsAgents"))).getSelectedItem();
		Group selectedGroup = (Group) this.dsGroups.getSelectedItem();
		AgentGroup check = agentsGroupDAO.findByAgentAndGroup(selectedAgent, selectedGroup);
		
		if(check != null)
		{
			MessageBox.Show(this, "Obavestenje", "Agent je vec dodat u grupu!");
			return;
		}
		
		ag = new AgentGroup();
		ag.setAgent(selectedAgent);
		ag.setGroup(selectedGroup);
		agentsGroupDAO.create(ag);

		bindAgentsForGroup();

		MessageBox.Show(this, "Obavestenje", String.format("Agent %s je dodat u %s grupu!", selectedAgent.getAgentFirstName() + " " + selectedAgent.getAgentLastName(), selectedGroup.getGroupName()));
	}

	private void bindGroupsList() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		groupDAO = SpringBeansLookup.getBean(GroupDAO.class);
		groups = groupDAO.selectAll();
		this.dsGroups.setSource(groups);
		this.dsGroups.bind(bcGroups);
	}

	private void bindAgentsForGroup() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		agentGroupDAO = SpringBeansLookup.getBean(AgentDAO.class);
		setGroupAgents(agentGroupDAO.findAgentByGroup((Group) this.dsGroups.getSelectedItem()));
		this.dsAgentsGroups.setSource(getGroupAgents());
		this.dsAgentsGroups.bind(bcAgentsGroups);
		
		groupAgent = new ArrayList<AgentGroup>();
	}

	public void set_selectionMode(boolean _selectionMode) {
		this._selectionMode = _selectionMode;
	}

	public boolean get_selectionMode() {
		return _selectionMode;
	}

	public void setSelectedAgent(Agent selectedAgent) {
		this.selectedAgent = selectedAgent;
	}

	public Agent getSelectedAgent() {
		return selectedAgent;
	}

	public void setGroupAgents(List<Agent> groupAgents) {
		this.groupAgents = groupAgents;
	}

	public List<Agent> getGroupAgents() {
		return groupAgents;
	}
}